﻿#include <graphics.h>
#include "Canva.h"
#include "externalArg.h"
#include"CommonDepence.h"
#include <conio.h>
#include <comdef.h>
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
#include <TLGCUDA.cuh>
#include <thread>
#include <time.h>
#include <CImg.h>
#include <Magick++.h>
namespace fs = std::filesystem;
using std::ofstream;
void start(int W, int H, int multiple, int split, int min, int max, std::string path, std::string initial);
void checkOperation(int multiple, Canva& img) {
	while (!rca::restart) {
		img.checkOperation(multiple);
	}
}
int rca::rscount = 0;
int main(int argc,char* argv[]) {
	/*
	int dev = 0;
	cudaDeviceProp devProp;
	cudaGetDeviceProperties(&devProp, dev);
	std::cout << "使用GPU device " << dev << ": " << devProp.name << std::endl;
	std::cout << "SM的数量：" << devProp.multiProcessorCount << std::endl;
	std::cout << "每个线程块的共享内存大小：" << devProp.sharedMemPerBlock / 1024.0 << " KB" << std::endl;
	std::cout << "每个线程块的最大线程数：" << devProp.maxThreadsPerBlock << std::endl;
	std::cout << "每个EM的最大线程数：" << devProp.maxThreadsPerMultiProcessor << std::endl;
	std::cout << "每个SM的最大线程束数：" << devProp.maxThreadsPerMultiProcessor / 32 << std::endl;
	std::cin.get();
	*/
	int W = 256;
	int H = 256;
	int multiple = 8;
	int split = 1;
	int min = 0;
	int max = 4500;
	std::string path = "test";
	std::string initial = "noise.png";
	int savePara = 0;
	if ( argc > 1 ) {
		W = atoi(argv[1]);
		H = atoi(argv[2]);
		multiple = atoi(argv[3]);
		path = argv[4];
		initial = argv[5];
		split = atoi(argv[6]);
		min = atoi(argv[7]);
		max  = atoi(argv[8]);
		savePara = 1;
	}
	else {
		
		std::cout << "输入窗口宽度：";
		std::cin >> W;
		std::cout << "输入窗口高度：";
		std::cin >> H;
		std::cout << "输入像素大小：";
		std::cin >> multiple;
		std::cout << "输入储存文件夹名：";
		std::cin >> path;
		std::cout << "输入初始图像名：";
		std::cin >> initial;
		std::cout << "抽帧分割数？";
		std::cin >> split;
		std::cout << "显示区间：从：";
		std::cin >> min;
		std::cout << "到：";
		std::cin >> max;
	}
	do{
		start(W, H, multiple, split, min, max, path,initial);
	} while (rca::restart);
	return 0;

}

void start(int W,int H,int multiple,int split,int min,int max,std::string path,std::string initial) {
		rca::restart = 0;
		Canva Paint = Canva(W, H, 0);
		CImg<char> noise(initial.c_str());
		noise.resize(W, H,1,3,6);
		Paint.updateWithIMG(noise);
		Paint.initDisplay();
		Paint.updateClassInit(multiple);
		int count = 0;
		std::thread mouseChange(checkOperation, multiple, std::ref(Paint));
		mouseChange.detach();

		std::string filename = std::string("./OutPut").append("/").append(path).append("/").append(std::to_string(rca::rscount)).append("/");
		std::string pfilename = filename;
		fs::create_directories(pfilename);
		pfilename.append("Matrix.txt");
		std::ifstream  src("Matrix.txt", std::ios::binary);
		std::ofstream  dst(pfilename, std::ios::binary|std::ios::out);
		int i = src.is_open();
		i = dst.is_open();
		dst << src.rdbuf();
		src.close();
		dst.close();

		while (count < max) {
			if (rca::restart) {
				rca::rscount++;
				return;
			}
			if (count >= min) {
				std::string sfilename = filename;
				Paint.updateFromClass();
				Paint.updateDisplay();
				sfilename.append("images/").append(std::to_string(count % split)).append("/");
				fs::create_directories(sfilename);
				sfilename.append(std::to_string(count)).append(".jpg");
				std::ofstream file;
				file.open(sfilename, std::ios::out);
				file.close();
				Paint.saveJpeg(sfilename.c_str());
			}
			Paint.updateClass();
			count++;
		}
}
